package com.moli.iov.sys.component;

import com.moli.iov.util.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * <p>
 * token
 * </p>
 *
 * @author zql
 * @since 2019/9/26 12:48
 */
@Component
public class TokenUtils {

  @Value("${pwd.salt}")
  private String salt;

  @Value("${jwt.expiration}")
  private Long expiration;

  @Value("${jwt.tokenHead}")
  private String tokenHead;

  /**
   * head + token
   * @param account
   * @return
   */
  public String generateTokenAndHead(String account){
    String token = generateToken(account);
    return tokenHead + token;
  }

  /**
   * token
   * @param account
   * @return
   */
  public String generateToken(String account){
    return JwtTokenUtil.generateToken(account, salt, expiration);
  }

  /**
   * 获取账号
   * @param token
   * @return
   */
  public String getAccountFromToken(String token){
    return getAccount(token.substring(this.tokenHead.length()));
  }

  /**
   * 获取账号
   * @param token
   * @return
   */
  public String getAccount(String token){
    return JwtTokenUtil.getUserNameFromToken(token, salt);
  }
}
